package com.logic.modular.game.player.fish.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.comment.dubbo.service.PlayerFishServiceClient;
import com.comment.exception.ServiceException;
import com.logic.comment.util.RedisUtils;
import com.logic.config.netty.web.service.MessageService;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.game.player.fish.mapper.PlayerFishMapper;
import com.comment.pojo.domain.logic.domain.PlayerFish;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 玩家钓鱼积分(PlayerFish)表服务实现类
 *
 * @author cxy
 * @since 2024-11-29 16:15:14
 */
@Slf4j
@Service
@DubboService
@Transactional
public class PlayerFishServiceImpl extends ServiceImpl<PlayerFishMapper, PlayerFish>
    implements PlayerFishServiceClient
{
    private static final String FISH_ACT = "fishAct";
    @Resource
    private RedisUtils redisUtils;

    @Override
    public void actionCheck(Long playerId, String url) {
        if (!url.contains(FISH_ACT)) {
            PlayerFish playerFish = obtainPlayerFish(playerId);
            if (playerFish.getStatus() == 1) {
                throw new ServiceException("请先离开钓鱼");
            }
        }
    }

    @Override
    public PlayerFish obtainPlayerFish(Long playerId) {
        PlayerFish byId = getById(playerId);
        if (byId == null) {
            PlayerFish build = PlayerFish.builder()
                    .uid(playerId)
                    .fishCount(BigDecimal.ZERO)
                    .fishTimeToday(0L)
                    .fishCountToday(BigDecimal.ZERO)
                    .status(0)
                    .build();
            if (build.insert()) {
                byId = getById(playerId);
                redisUtils.set(String.format("player:%d:fish", playerId), byId, 1L, TimeUnit.DAYS);
            }
        }
        return byId;
    }

    public boolean updateById(PlayerFish playerFish) {
        Long playerId = playerFish.getUid();
        boolean b = playerFish.updateById();
        if (b) {
            PlayerFish byId = getById(playerId);
            redisUtils.set(String.format("player:%d:fish", playerId), byId, 1L, TimeUnit.DAYS);
            MessageService.sendMessageToUser(playerId, MessageService.updateJsonObject("playerFish", byId));
        }
        return b;
    }

    @Override
    public PlayerFish start(Long playerId) {
        PlayerFish playerFish = obtainPlayerFish(playerId);
        BigDecimal multiply = CoreConfigServiceImpl.getCache(9L).getValue1().multiply(new BigDecimal(CoreConfigServiceImpl.getCache(9L).getValue2()));
        if (playerFish.getFishCountToday().compareTo(multiply) < 0) {
            playerFish.setStatus(1);
            playerFish.setFishStartTime(new Date());
            updateById(playerFish);
        }
        return playerFish;
    }


    @Override
    public PlayerFish end(Long playerId) {
        PlayerFish playerFish = obtainPlayerFish(playerId);
        if (playerFish.getStatus() == 1) {
            BigDecimal value1BigDecimal = CoreConfigServiceImpl.getCache(9L).getValue1();

            String value2 = CoreConfigServiceImpl.getCache(9L).getValue2();
            BigDecimal value2BigDecimal = new BigDecimal(value2);
            long value2Long = Long.parseLong(value2);


            // 开始当天的截至时间
            Date fishStartTime = playerFish.getFishStartTime();
            DateTime endOfFishStart = DateUtil.endOfDay(fishStartTime);
            long fishStartDayBetweenMinute = DateUtil.between(fishStartTime, endOfFishStart, DateUnit.MINUTE);

            // 今天的截至时间
            Date date = new Date();
            DateTime endBeginOfDay = DateUtil.beginOfDay(date);
            long fishEndOfDayBetweenMinute = DateUtil.between(endBeginOfDay, date, DateUnit.MINUTE);

            // 间隔天数
            long betweenDay = DateUtil.between(endOfFishStart, endBeginOfDay, DateUnit.DAY);
            if (betweenDay > 0) {
                // 可获取积分的时长
                long min;
                // 获取的积分
                BigDecimal fishCount;

                // 第一天的
                min = Math.min(fishStartDayBetweenMinute, value2Long);
                fishCount = value1BigDecimal.multiply(new BigDecimal(min));
                // 累计积分
                playerFish.setFishCount(playerFish.getFishCount().add(fishCount));

                // 结算间隔天数
                BigDecimal dayMax = value1BigDecimal.multiply(value2BigDecimal);
                fishCount = dayMax.multiply(new BigDecimal(betweenDay));
                playerFish.setFishCount(playerFish.getFishCount().add(fishCount));

                min = Math.min(fishEndOfDayBetweenMinute, value2Long);
                fishCount = value1BigDecimal.multiply(new BigDecimal(min));
                // 累计积分
                playerFish.setFishCount(playerFish.getFishCount().add(fishCount));

            }
            if (betweenDay == 0) {
                // 钓鱼时长
                long fishTimeToday = playerFish.getFishTimeToday() + fishStartDayBetweenMinute;
                // 可获取积分的时长
                long min = Math.min(fishTimeToday, value2Long);
                // 获取的积分
                BigDecimal fishCountToday = value1BigDecimal.multiply(new BigDecimal(min));

                playerFish.setFishTimeToday(fishTimeToday);
                playerFish.setFishCountToday(fishCountToday);
                playerFish.setFishCount(playerFish.getFishCount().add(fishCountToday));
            }

            playerFish.setStatus(0);
            updateById(playerFish);
        }
        return playerFish;
    }
}
